package com.sample.ch4session.sec2cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/cookieRead.do")
public class CookieRead extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        // 并未读取到idea生成的cookie
        Cookie[] cookies = req.getCookies();
        if (cookies!=null) {
            for (Cookie cookie : cookies) {
                // 生成的String不会换行，要使用html元素才会换行
               // out.println(cookie.getName()+":"+cookie.getValue());
                out.println("<p>"+cookie.getName()+":"+cookie.getValue()+"</p>");
            }
        }
        out.close();
    }
}
